import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutterapp/model/favorites.dart';
// import 'package:flutterapp/screens/favorites.dart';

class HomePage extends StatelessWidget {
  static const routeName = '/';

  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
        itemCount: 100,
        cacheExtent: 20.0,
        controller: ScrollController(),
        padding: const EdgeInsets.symmetric(vertical: 10),
        itemBuilder: (context, index) => ItemTile(index),
      ),
    );
  }
}

class ItemTile extends StatelessWidget {
  final int itemNo;

  const ItemTile(this.itemNo, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final favoritesList = context.watch<Favorites>();

    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: ListTile(
        leading: CircleAvatar(
          backgroundImage:
                    NetworkImage("https://www.itying.com/images/flutter/3.png"),
        ),
        title: Text(
          '涛哥$itemNo：刚才 leading 比较特殊  返回的是组件。所以属性 就比较复杂<br>'+
          '你固定的组件  属性固定，就有说明。什么属性什么意思',
          key: Key('text_$itemNo'),
        ),
        trailing: IconButton(
          key: Key('icon_$itemNo'),
          icon: favoritesList.items.contains(itemNo)
              ? const Icon(Icons.favorite)
              : const Icon(Icons.favorite_border),
          onPressed: () {
            !favoritesList.items.contains(itemNo)
                ? favoritesList.add(itemNo)
                : favoritesList.remove(itemNo);
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(
                content: Text(favoritesList.items.contains(itemNo)
                    ? 'Added to favorites.'
                    : 'Removed from favorites.'),
                duration: const Duration(seconds: 1),
              ),
            );
          },
        ),
      ),
    );
  }
}
// import 'package:flutter/material.dart';
// import 'package:provider/provider.dart';
// import 'package:flutterapp/models/favorites.dart';
// import 'package:flutterapp/screens/favorites.dart';
// import 'package:flutterapp/screens/home.dart';

// void main() => runApp(new TestingApp());

// class TestingApp extends StatelessWidget {
//   const TestingApp({Key? key}) : super(key: key);

//   @override
//   Widget build(BuildContext context) {
//     return ChangeNotifierProvider<Favorites>(
//       create: (context) => Favorites(),
//       child: MaterialApp(
//         title: 'Testing Sample',
//         theme: ThemeData(
//           primarySwatch: Colors.blue,
//           visualDensity: VisualDensity.adaptivePlatformDensity,
//         ),
//         routes: {
//           HomePage.routeName: (context) => const HomePage(),
//           FavoritesPage.routeName: (context) => const FavoritesPage(),
//         },
//         initialRoute: HomePage.routeName,
//       ),
//     );
//   }
// }